package com.zxl.app.service;

import com.zxl.common.mapper.SysSmsCodeMapper;
import com.zxl.common.model.bo.sms.SmsCheck;
import com.zxl.common.model.pojo.SysSmsCode;
import com.zxl.common.service.SysSmsCodeService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Map;

/**
 * @author zxw
 * @date 2023/4/5
 */
@Service
public class SmsService {
    @Resource
    private SysSmsCodeService sysSmsCodeService;
    @Resource
    private SysSmsCodeMapper sysSmsCodeMapper;

    public void sendSms(String phone, Map<String, Object> param, String templateCode) {
        SysSmsCode sysSmsCode = new SysSmsCode();
        sysSmsCode.setUsed(false);
        sysSmsCode.setCode((String) param.get("code"));
        sysSmsCode.setPhone(phone);
        sysSmsCode.setEndTime(System.currentTimeMillis() + 1000*60*15);
        sysSmsCode.setCodeType(1);
        sysSmsCodeMapper.insert(sysSmsCode);

        sysSmsCodeService.send(phone,param,templateCode);
    }

    public void checkCode(SmsCheck smsCheck) {
        String code = smsCheck.getCode();
        String phone = smsCheck.getPhone();
        Integer codeType = smsCheck.getCodeType();
        sysSmsCodeService.check(code,phone,codeType);
    }
}
